package com.feifan.consumer.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.feifan.api.annotation.LogType;
import com.feifan.api.annotation.SystemLog;
import com.feifan.api.annotation.UserLoginToken;
import com.feifan.api.dto.OmsReturnApplyQueryParam;
import com.feifan.api.dto.OmsUpdateStatusParam;
import com.feifan.api.model.OmsOrderReturnApply;
import com.feifan.api.service.IOmsOrderReturnApplyService;
import com.feifan.common.CommonPage;
import com.feifan.common.CommonResult;
import com.feifan.provider.mapper.OmsOrderReturnApplyMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.annotation.RequestScope;

import java.util.List;

/**
 * @program: FinalProject
 * @author: Xiaofan
 * @createTime: 2021-08-08 09:47
 * @description: Functions of this class is
 **/
@RestController
@Api(tags = "OmsOrderReturnApplyController",description = "订单退货申请管理")
@RequestMapping("/returnApply")
@CrossOrigin
public class OmsOrderReturnApplyController {

    //dubbo做法
    @Reference(
            version = "1.0.0",
            interfaceName = "com.feifan.api.service.IOmsOrderReturnApplyService",
            interfaceClass = IOmsOrderReturnApplyService.class,
            timeout = 170000
    )
    private IOmsOrderReturnApplyService service;

    @ApiOperation(value = "分页查询退货申请")
    @GetMapping(value = "/list")
    @UserLoginToken
    @ResponseBody
    public CommonResult<CommonPage<OmsOrderReturnApply>> list(OmsReturnApplyQueryParam queryParam,
                                                              @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                                              @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize){
        return CommonResult.success(service.list(queryParam,pageNum,pageSize));
    }

    @ApiOperation(value = "获取退货申请详情")
    @GetMapping(value = "/{id}")//因为这里的id可能有多个，所以一定要用{id} 而不可以直接用id来，
    @UserLoginToken
    @ResponseBody
    public CommonResult getItem(@PathVariable Long id){
        return CommonResult.success(service.getItem(id));
    }

    @ApiOperation(value = "批量删除退货申请")
    @ResponseBody
    @PostMapping(value = "/delete")
    @UserLoginToken
    @SystemLog(description = "批量删除退货申请",type = LogType.ORDER_DELETE_RETURN)
    public CommonResult delete(@RequestParam(value = "ids") List<Long> ids){
        Integer sum=service.delete(ids);
        if(sum>=0){
            return CommonResult.success(sum);
        }
        return CommonResult.failed();
    }

    @ApiOperation(value = "修改申请订单状态")
    @ResponseBody
    @PostMapping(value = "/update/status/{id}")
    @UserLoginToken
    @SystemLog(description = "修改申请订单状态",type = LogType.ORDER_UPDATE_RETURN_STATUS)
    public CommonResult updateStatus(@PathVariable Long id,@RequestBody OmsUpdateStatusParam status){
        CommonResult commonResult;
        try{
            service.updateStatus(id,status);
            commonResult=CommonResult.success(true);
        }
        catch (Exception e){
            e.printStackTrace();
            commonResult=CommonResult.failed();
        }
        return commonResult;
    }
}